<template>
  <div class="login">
    <login-close></login-close>
    <form class="login-form">
      <p class="login-title">帐号密码登录</p>
      <phone ></phone>
      <pwd></pwd>
      <!--<captcha @getCaptcha="getCaptcha"></captcha>-->
      <div class="item forget-item">
        <a class="forgetPwd font1" @click="goToTarget('find')">忘记密码？</a>
      </div>
      <div class="item"><p class="form-btn btn3" @click="submit">登录</p></div>
      <p class="btn-under font1" @click="goToTarget('login')">动态验证码登录</p>
    </form>
  </div>
</template>
<script type="text/ecmascript-6">
  import phone from '../../component/login/phoneIpt.vue'
//  import captcha from '../../component/login/captcha.vue'
//  import chaKey from '../../component/login/kaptchaKey.vue'
  import pwd from '../../component/login/pwdIpg.vue'
  import loginClose from '../../component/login/close.vue'
  import {loginService} from '../../service/loginService'
  import {commonService} from '../../service/commonService'

  export default {
    props: [],
    components: {"phone": phone, "pwd": pwd, 'login-close': loginClose },
    data () {
      return {
        msg: {type: 0 },
        phoneNo: '', //手机号
        kaptchaValue: '', //用户输入的图片验证码
        pwd: '', //密码
        platform: 'WXH5'

      }
    },
    mounted: function () {
      let that = this
      that.platform = loginService.getPlatForm();
      that.$store.state.loginStore.platform = that.platform;
    },
    methods: {
      submit () { //表单提交的方法
        let that = this
        let mess = '';
        // 赋值区域 Begin
        that.phoneNo =that.$store.state.loginStore.phoneNo;
        that.pwd =that.$store.state.loginStore.password;
        // 赋值区域 end
        mess = loginService.formValidate(1, 0, 0, 1,{phoneNo: that.phoneNo, smsValue: that.smsValue, pwd: that.pwd});
        if(typeof mess != 'undefined') {
          if(mess.trim('').length > 0){
            that.pop(mess);
          }
        }
        else {
//          document.getElementById('yes').style.display = 'block';
          let params = {
              platform: that.platform,
              phone: that.phoneNo,
              password: that.pwd
          }
          loginService.pwdLogin(params).then(function (res) {
            // console.log('pwd login', res)
            if(res.data.success){
              localStorage.token = res.data.datas;
              let JWT = res.data.datas.split('.');
              let info = JWT[1];
                loginService.packageUserInfo(info);
              // 清空倒计时
              if(that.$store.state.loginStore.timer > 0){
                clearInterval(that.$store.state.loginStore.timer)
              }
              that.goToTarget('');//跳转到来的页面
            }else{
              //if(res.data.code >= 513110 && res.data.code <= 513120){
//              document.getElementById('yes').style.display = 'none';
                that.pop(res.data.message);
              //}
              //------------------
            }
          });
        }
      },
      pop (mess) { //弹框的方法
        let that = this
          commonService.autoCloseModal(that, mess, 1);
      },
      goToTarget (path) { //返回登录
        let that = this

        if(typeof path != 'undefined' && path.trim().length > 0){
          this.$router.replace({name: path})
        }
        else{
            if(that.$store.state.redirect_uri && that.$store.state.redirect_uri.name){
                let name = that.$store.state.redirect_uri.name, params = that.$store.state.redirect_uri.params;
                that.$router.replace({name: name, params: params})
                that.$store.state.redirect_uri = {}
            }else if(localStorage.redirectUrl && localStorage.redirectUrl!="undefined"){
                let url = localStorage.redirectUrl
                localStorage.removeItem('redirectUrl')
                window.location.href = url
            }else{
                that.$router.replace({name: 'home'})
            }
        }
      }
    }
  }
</script>
<style lang="less">
  @import "../../assets/css/login.less";
</style>